package com.yaodian100.ec.order.entity;

/**
 * @author jinwei.lin
 *
 */
import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import com.yaodian100.core.common.entity.BaseEntity;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.Specification;
import com.yaodian100.ec.shelf.entity.Shelf;
import com.yaodian100.ec.shopping.entity.CartOrderProductDetail;

@Entity
@Table(name = "EC_ORDER_DETAIL", uniqueConstraints = { @UniqueConstraint(columnNames = { "order_id", "prod_seq",
		"detail_seq" }) })
public class OrderProductDetail extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 322605749533649805L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_ec_order_detail")
	@SequenceGenerator(name = "seq_ec_order_detail", sequenceName = "seq_ec_order_detail")
	@Column(name = "order_detail_id")
	private Long orderDetailId;

	@Column(name = "order_prod_id", insertable = false, updatable = false)
	private Long orderProdId;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "order_prod_id", nullable = false, updatable = false)
	private OrderProduct orderProd;

	@Column(name = "order_id", length = 13, insertable = false, updatable = false)
	private String orderId;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "order_id", nullable = false)
	private Orders order;

	@Column(name = "prod_seq", nullable = false)
	private int prodSeq;

	@Column(name = "detail_seq", nullable = false)
	private int detailSeq;

	@Column(name = "prod_id", length = 10, nullable = false, updatable = false)
	private String prodId;

	@Column(name = "prod_spec_id", length = 10, nullable = false, updatable = false)
	private String prodSpecId;

	@Column(name = "prod_name", length = 500, nullable = false)
	private String prodName;

	@Column(name = "prod_spec_name", length = 300, nullable = false)
	private String prodSpecName;

	@Column(name = "detail_tp", length = 5, nullable = false)
	private String detailTp;

	@Column(name = "ec_cost", columnDefinition = "numeric(10,2)")
	private BigDecimal ecCost = BigDecimal.ZERO;

	@Column(name = "erp_cost", columnDefinition = "numeric(10,2)")
	private BigDecimal erpCost = BigDecimal.ZERO;

	@Column(name = "qty")
	private int qty;

	@Column(name = "amount", columnDefinition = "numeric(10,2)")
	private BigDecimal amount = BigDecimal.ZERO;

	@Column(name = "vend_id", length = 6)
	private String vendId = " ";
	@Column(name = "unit_price", columnDefinition = "numeric(10,2)")
	private BigDecimal unitPrice = BigDecimal.ZERO;

	/**
	 * 记录该商品之单一商品重量
	 */
	@Column(name = "UNIT_WEIGHT", columnDefinition = "NUMERIC(10, 2)")
	private BigDecimal unitWeight = BigDecimal.ZERO;

	/**
	 * 赠送赠物金点数
	 */
	@Column(name = "GIVE_BONUS")
	private int giveBonus = 0;
	@Column(name = "OUTER_PROD_ID", length = 255)
	private String outerProdId;
	@Column(name = "OUTER_SKU_ID", length = 255)
	private String outerSkuId;
	@Column(name = "OUTER_CID", length = 255)
	private String outerCid;

	/**
	 * @return the outerCid
	 */
	public String getOuterCid() {
		return outerCid;
	}

	/**
	 * @param outerCid the outerCid to set
	 */
	public void setOuterCid(String outerCid) {
		this.outerCid = outerCid;
	}

	/**
	 * @return the outerSkuId
	 */
	public String getOuterSkuId() {
		return outerSkuId;
	}

	/**
	 * @param outerSkuId the outerSkuId to set
	 */
	public void setOuterSkuId(String outerSkuId) {
		this.outerSkuId = outerSkuId;
	}

	/**
	 * @return the outerProdId
	 */
	public String getOuterProdId() {
		return outerProdId;
	}

	/**
	 * @param outerProdId the outerProdId to set
	 */
	public void setOuterProdId(String outerProdId) {
		this.outerProdId = outerProdId;
	}

	/**
	 * 出貨單號
	 */
	// @Column(name = "SHIPPING_OID")
	@Transient
	private String shippingOid;

	/**
	 * 出貨單細項 oid
	 */
	// @Column(name = "SHIPPING_LINE_OID")
	@Transient
	private Long shippingLineOid;

	/**
	 * 预计出货日 yyyy-MM-dd
	 */
	@Column(name = "EXPECT_SHIP_DATE", length = 10)
	private String expectShipDate;

	/**
	 * 是否允许COD,default Y
	 */
	// @Transient
	@Column(name = "CAN_USE_COD", length = 1)
	private String canUseCod;

	/**
	 * 记录该商品之单一商品体积
	 */
	@Column(name = "UNIT_VOLUME_WEIGHT", columnDefinition = "NUMERIC(10, 2)")
	private BigDecimal unitVolumeWeight = BigDecimal.ZERO;

	/**
	 * 退换货条件
	 */
	@Column(name = "RETURN_RULE", length = 5)
	private String returnRule = "07_15";

	/**
	 * 供应商订购后几日出货(default 0)
	 */
	@Column(name = "BO_TO_WH_DATE")
	private int boToWhDate = 0;

	/**
	 * 订购后几日出货(default 0)
	 */
	@Column(name = "DELIVERY_DAY")
	private int deliveryDay = 0;

	/**
	 * 主馆路径
	 */
	@Transient
	private String categoryPath;

	/**
	 * 主馆名称
	 */
	@Transient
	private String categoryName;

	/** cash */
	@Column(name = "cash", columnDefinition = "numeric(10,2)")
	// @Transient
	private BigDecimal cash = BigDecimal.ZERO;
	/**
	 * 记录该商品礼品卡使用比例(扣除运费，前几笔商品采用小数点第一位后无条件舍去的方式计算，最后一笔商品用减的)
	 */
	@Column(name = "GIFTCARD_BALANCE", columnDefinition = "NUMERIC(10, 2)")
	private BigDecimal giftcardBalance = BigDecimal.ZERO;

	@Column(name = "bonus", columnDefinition = "numeric(10,2)")
	private BigDecimal bonus = BigDecimal.ZERO;

	/** shipfare, 運費 */
	@Column(name = "shipfare", columnDefinition = "numeric(10,2)")
	private BigDecimal shipfare = BigDecimal.ZERO;

	@Column(name = "my_wallet", columnDefinition = "numeric(10,2)")
	private BigDecimal myWallet = BigDecimal.ZERO;

	@Column(name = "must_pay", columnDefinition = "numeric(10,2)")
	private BigDecimal mustPay = BigDecimal.ZERO;

	/** COD手續費 */
	@Column(name = "COD_FEE", columnDefinition = "numeric(10,2)")
	private BigDecimal codFee = BigDecimal.ZERO;

	@Column(name = "INSTALLMENT_FEE", columnDefinition = "numeric(10,2)")
	private BigDecimal installmentFee = BigDecimal.ZERO;

	/** discount, 折扣總計 */
	@Column(name = "discount", columnDefinition = "numeric(10,2)")
	private BigDecimal discount = BigDecimal.ZERO;

	/** coupon, 使用 coupon 的面額 */
	@Column(name = "coupon", columnDefinition = "numeric(10,2)")
	private BigDecimal coupon = BigDecimal.ZERO;

	/** default constructor */
	public OrderProductDetail() {
		super();
	}

	public OrderProductDetail(CartOrderProductDetail copd) {
		super();
		this.ecCost = copd.getEcCost();
		if ("6N".equals(copd.getDetailTp())) {
			this.detailTp = "6";
		} else {
			this.detailTp = copd.getDetailTp();
		}
		this.prodId = copd.getProdId();
		this.prodName = copd.getProdName();
		this.prodSpecId = copd.getProdSpecId();
		this.prodSpecName = copd.getProdSpecName();
		this.qty = copd.getQty();
		// CartOrderProductDetail.amount 為售價
		this.amount = copd.getAmount().multiply(BigDecimal.valueOf(copd.getQty()));
		this.unitPrice = copd.getAmount();
		this.unitWeight = copd.getWeight();
		this.vendId = copd.getVendId();
		this.unitVolumeWeight = copd.getVolume();
	}

	public OrderProductDetail(Product prod, Specification spec, Shelf shelf) {
		super();
		this.ecCost = prod.getProductCost();
		if (shelf.getShelfPrice().compareTo(BigDecimal.ZERO) == 0) {
			this.detailTp = "2";
		} else {
			this.detailTp = "0";
		}
		this.prodId = prod.getProductId();
		this.prodName = prod.getProductName();
		this.prodSpecId = spec.getSpecificationId().toString();
		this.prodSpecName = spec.getSpecificationName();
		this.qty = 1;
		this.amount = shelf.getShelfPrice(); // 因為 qty=1
		this.unitPrice = shelf.getShelfPrice();
		this.unitWeight = prod.getProductWeight();
		this.vendId = prod.getSupplierId();
		this.unitVolumeWeight = prod.getProductVolume();
		this.returnRule = prod.getReturnRule();
		this.canUseCod = StringUtils.defaultIfEmpty(prod.getCanUseCod(), "Y");
		if ((prod.getProductType() == 1) && (null != prod.getExpectShipDate())) {
			this.expectShipDate = EcConstant.PATTERN_DATE_FORMAT.format(prod.getExpectShipDate());
		}
		if (prod.getProductType() != 1) {
			if (StringUtils.equals(prod.getUseDeliveryDay(), "Y")) {
				this.deliveryDay = prod.getDeliveryDay();
			} else {
				this.deliveryDay = 0;
			}
			if (StringUtils.equals(prod.getIsScm(), "B")) {
				this.boToWhDate = prod.getDeliveryDay();
			} else {
				this.boToWhDate = 0;
			}
		}
	}

	/**
	 * @return the order
	 */
	public Orders getOrder() {
		return order;
	}

	/**
	 * @param order the order to set
	 */
	public void setOrder(Orders order) {
		this.order = order;
	}

	/**
	 * @return the orderProd
	 */
	public OrderProduct getOrderProd() {
		return orderProd;
	}

	/**
	 * @param orderProd the orderProd to set
	 */
	public void setOrderProd(OrderProduct orderProd) {
		this.orderProd = orderProd;
	}

	/**
	 * @return the unitPrice
	 */
	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	/**
	 * @param unitPrice the unitPrice to set
	 */
	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public Long getOrderDetailId() {
		return orderDetailId;
	}

	public void setOrderDetailId(Long orderDetailId) {
		this.orderDetailId = orderDetailId;
	}

	public Long getOrderProdId() {
		return orderProdId;
	}

	public void setOrderProdId(Long orderProdId) {
		this.orderProdId = orderProdId;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public int getProdSeq() {
		return prodSeq;
	}

	public void setProdSeq(int prodSeq) {
		this.prodSeq = prodSeq;
	}

	public int getDetailSeq() {
		return detailSeq;
	}

	public void setDetailSeq(int detailSeq) {
		this.detailSeq = detailSeq;
	}

	public String getProdId() {
		return prodId;
	}

	public void setProdId(String prodId) {
		this.prodId = prodId;
	}

	public String getProdSpecId() {
		return prodSpecId;
	}

	public void setProdSpecId(String prodSpecId) {
		this.prodSpecId = prodSpecId;
	}

	public String getProdName() {
		return prodName;
	}

	public void setProdName(String prodName) {
		this.prodName = prodName;
	}

	public String getProdSpecName() {
		return prodSpecName;
	}

	public void setProdSpecName(String prodSpecName) {
		this.prodSpecName = prodSpecName;
	}

	public String getDetailTp() {
		return detailTp;
	}

	public void setDetailTp(String detailTp) {
		this.detailTp = detailTp;
	}

	public BigDecimal getEcCost() {
		return ecCost;
	}

	public void setEcCost(BigDecimal ecCost) {
		this.ecCost = ecCost;
	}

	public BigDecimal getErpCost() {
		return erpCost;
	}

	public void setErpCost(BigDecimal erpCost) {
		this.erpCost = erpCost;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public String getVendId() {
		return vendId;
	}

	public void setVendId(String vendId) {
		this.vendId = vendId;
	}

	public BigDecimal getUnitWeight() {
		return unitWeight;
	}

	public void setUnitWeight(BigDecimal unitWeight) {
		this.unitWeight = unitWeight;
	}

	public int getGiveBonus() {
		return giveBonus;
	}

	public void setGiveBonus(int giveBonus) {
		this.giveBonus = giveBonus;
	}

	public String getShippingOid() {
		return this.shippingOid;
	}

	public void setShippingOid(String shippingOid) {
		this.shippingOid = shippingOid;
	}

	public Long getShippingLineOid() {
		return this.shippingLineOid;
	}

	public void setShippingLineOid(Long shippingLineOid) {
		this.shippingLineOid = shippingLineOid;
	}

	public String getExpectShipDate() {
		return expectShipDate;
	}

	public void setExpectShipDate(String expectShipDate) {
		this.expectShipDate = expectShipDate;
	}

	public String getCanUseCod() {
		return canUseCod;
	}

	public void setCanUseCod(String canUseCod) {
		this.canUseCod = canUseCod;
	}

	public BigDecimal getUnitVolumeWeight() {
		return unitVolumeWeight;
	}

	public void setUnitVolumeWeight(BigDecimal unitVolumeWeight) {
		this.unitVolumeWeight = unitVolumeWeight;
	}

	public String getReturnRule() {
		// 相容於舊訂單資料，回傳預設值
		if ((returnRule == null) || "".equals(returnRule)) {
			return "07_15";
		} else {
			return returnRule;
		}
	}

	public void setReturnRule(String returnRule) {
		this.returnRule = returnRule;
	}

	public int getBoToWhDate() {
		return boToWhDate;
	}

	public int getDeliveryDay() {
		return deliveryDay;
	}

	public void setBoToWhDate(int boToWhDate) {
		this.boToWhDate = boToWhDate;
	}

	public void setDeliveryDay(int deliveryDay) {
		this.deliveryDay = deliveryDay;
	}

	public String getCategoryPath() {
		return categoryPath;
	}

	public void setCategoryPath(String categoryPath) {
		this.categoryPath = categoryPath;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public BigDecimal getGiftcardBalance() {
		return giftcardBalance;
	}

	public void setGiftcardBalance(BigDecimal giftcardBalance) {
		this.giftcardBalance = giftcardBalance;
	}

	public BigDecimal getBonus() {
		return bonus;
	}

	public void setBonus(BigDecimal bonus) {
		this.bonus = bonus;
	}

	public BigDecimal getShipfare() {
		return shipfare;
	}

	public BigDecimal getMyWallet() {
		return myWallet;
	}

	public BigDecimal getMustPay() {
		return mustPay;
	}

	public BigDecimal getCodFee() {
		return codFee;
	}

	public BigDecimal getInstallmentFee() {
		return installmentFee;
	}

	public void setShipfare(BigDecimal shipfare) {
		this.shipfare = shipfare;
	}

	public void setMyWallet(BigDecimal myWallet) {
		this.myWallet = myWallet;
	}

	public void setMustPay(BigDecimal mustPay) {
		this.mustPay = mustPay;
	}

	public void setCodFee(BigDecimal codFee) {
		this.codFee = codFee;
	}

	public void setInstallmentFee(BigDecimal installmentFee) {
		this.installmentFee = installmentFee;
	}

	public BigDecimal getCash() {
		return cash;
	}

	public void setCash(BigDecimal cash) {
		this.cash = cash;
	}

	public BigDecimal getDiscount() {
		return discount;
	}

	public void setDiscount(BigDecimal discount) {
		this.discount = discount;
	}

	public BigDecimal getCoupon() {
		return coupon;
	}

	public void setCoupon(BigDecimal coupon) {
		this.coupon = coupon;
	}
}
